import 'package:flutter/material.dart';
import 'package:flutter_lifecycle/flutter_lifecycle.dart';
import 'package:flutter_lifecycle_example/log.dart';

class Sub1 extends StatefulWidget {
  String? title;

  Sub1({Key? key, this.title}) : super(key: key);

  @override
  State createState() {
    return _Sub1State();
  }
}

class _Sub1State extends State<Sub1> {
  @override
  void initState() {
    super.initState();
    logger.d('initState');
  }

  @override
  void dispose() {
    super.dispose();
    logger.d('dispose');
  }

  @override
  Widget build(BuildContext context) {
    logger.d('build');
    return  BasePageLifecycle(
      onPageShow: (){
        logger.d('onPageShow');
      },
      onPageHide: (){
        logger.d('onPageHide');
      },
      onBackground: (){
        logger.d('onBackground');
      },
      onForeground: (){
        logger.d('onForeground');
      },
      onInactive: (){
        logger.d('onInactive');
      },
      onPageCreate: (){
        logger.d('onPageCreate');
      },
      onPageDispose: (){
        logger.d('onPageDispose');
      },
      
      
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title ?? "Sub1"),
        ),
        body: Container(
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              TextButton(
                onPressed: () {
                  Navigator.of(context).pushReplacementNamed('nav2');
                },
                child: const Text("replace"),
              ),
              TextButton(
                onPressed: () {
                  var route = ModalRoute.of(context);
                  Navigator.of(context).pushNamed('nav2');
                  Future.delayed(const Duration(milliseconds: 300)).then((value) => Navigator.of(context).removeRoute(route!));
                },
                child: const Text('pushAndRemove'),
              ),
              TextButton(
                onPressed: (){
                  Navigator.of(context).pushNamed('nav2');
                  // Navigator.of(context).pushNamed('sub1');
                },
                child: const Text('push'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
